package com.fairytask.engine.components.table
{
	import com.fairytask.data.BaseData;
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.Scroll.ScrollPanel;
	import com.fairytask.engine.components.Scroll.ScrollSkin;
	import com.fairytask.engine.define.ColorDefine;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.utils.draw.DrawAPI;
	import com.fairytask.engine.utils.draw.DrawStyle;
	import com.fairytask.managers.ResManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * 表格类 用来显示文字类型的列表 
	 * @author hanjy
	 * 取得link内容的方法
	 * private function onItemLink(evt:TableEvent,pTarget:*=null):void
	 *	{
	 *		trace(evt.data.GetProperties(TableEvent.ITEM_LINK));
	 *	}
	 */	
	public class Table extends BaseComponent
	{
		//属性
		private var _titleSkin		:Sprite;
		private var _itemSkinArr	:Array;
		private var _maxWidth		:int	=	0;
		//显示
		private var _scrollPanel	:ScrollPanel;
		private var _mainSpace		:Sprite	=	new Sprite(); //主容器
		private var _titleSpace		:Sprite	=	new Sprite();
		private var _bodySpace		:Sprite	=	new Sprite();
		private var _selectShap		:Sprite	=	new Sprite();
		//数据
		private var _data			:Array	=	[];
		private var _cols			:int	=	0;
		private var _titleData		:Array	=	[];
		private var _titleArr		:Array	=	[];
		private var _rowArr			:Array	=	[];
		private var _sizeData		:Array	=	[];
		//表头信息
		private var _titleArray		:Array	=[];
		
		private var _callBack		:BaseData;
		
		private var _selectRow		:TableRow	=	null;
		
		public static var colSplit:int	=	1;
		public static var rowSplit:int	=	1;	//这值是内容距离顶部标题的距离
		
		//默认行高19
		private var _lineHeight	:int	=	19;
		//出去行高19的行间距，默认0
		private var _rowSpace		:int	=	0;
		
		/**
		 * 表格初始化 
		 * @param _w 宽
		 * @param _h 高
		 * @param _titleArr 表格头，string数组
		 * @param _skArr 皮肤 string 数组
		 * @param _isPage 是否分页
		 */		
		public function Table(_w:int,_h:int,_titleStrArr:Array,_skArr:Array=null,_sizeArr:Array=null,_isPage:Boolean=false)
		{
			_callBack = new BaseData();
			if(_sizeArr!=null)
			{
				_sizeData	=	_sizeArr;	//自定义大小
			}
			
			_selectShap.alpha	=	0;
			if(_skArr!=null)
			{
				_titleSkin		=	ResManager.newResClass(_skArr.shift()) as Sprite;		//提取素材资源
				while(_skArr.length>0)
				{
					_itemSkinArr.push(ResManager.newResClass(_skArr.shift()));
				}
			}
			_titleSpace.y	=	5;			//列标题	空间
			addChild(_titleSpace);
			//滚动条 区域
			_scrollPanel	=	new ScrollPanel(new Rectangle(0,20,_w,_h-30),_bodySpace,new ScrollSkin("vScrol_4","vScrol_5"));
			_scrollPanel.y	=	25;
			addChild(_scrollPanel);
			_titleArray	=	_titleStrArr
			initGUI(_titleArray);
			initEvent();
		}
		
		private function initEvent():void
		{
			//adde
		}
		
		private function initGUI(_titleStrArr:Array):void
		{
			//清空容器
			while(_titleSpace.numChildren>0)
			{
				_titleSpace.removeChildAt(0);
			}
			
			_cols	=	_titleArr.length;		//列数
			var tmpRow:TableRow	=	new TableRow(true);		
			for(var idx:int=0;idx<_titleStrArr.length;idx++)
			{
				var tmpItem:TableItem	=	new TableItem(_titleStrArr[idx],true,_sizeData[idx]);
				var tmpArray	:Array	=	new Array();
				tmpArray.push(Filter.EXP_FILTER);
				tmpItem.setBorder(tmpArray);
				tmpRow.addItem(tmpItem);
				_titleArr.push(tmpItem);
			}
			_titleSpace.addChild(tmpRow);
		}
		/**
		 *	更新表头 
		 * @param column		更新列号	
		 * @param newString	新字符串
		 * 
		 */		
		public  function updateTitle(column:int,newString:String):void
		{
			//更新表头
			_titleArray[column-1]	=	newString;
			initGUI(_titleArray);
		}
		
		public function addRowItem(pRow:TableRow,rowData:BaseData=null):void
		{
			pRow.addEventListener(MouseEvent.CLICK,onItemClick);//事件
			pRow.pos	=	_rowArr.length;
			_rowArr.push(pRow);	
			pRow.data	=	rowData;
			pRow.y	=	_bodySpace.height + rowSplit;
			_bodySpace.addChild(pRow);
			if(_maxWidth<_bodySpace.width)
			{
				_maxWidth	=	_bodySpace.width;
				if(_selectShap.numChildren==0)
				{
					//画选中效果
					DrawAPI.drawRect(_selectShap.graphics,DrawStyle.getRectStyle(0,0,_maxWidth,20),DrawStyle.getFillStyle(uint(ColorDefine.DISABLE),.7));
				}
			}
			updataBodySpace();
		}
		
		public function addRowAt(index:int,labelArr:Array,rowData:BaseData,strColor:String="FFFFFF"):void
		{
			var tmpRow:TableRow	=	new TableRow();		//一条数据
			for(var i:int=0;i<labelArr.length;i++)
			{
				var tmpItem:TableItem	=	new TableItem(labelArr[i],false,_sizeData[i],strColor);//TableItem就是一个Label		_sizeData[idx]列之间间隙
				tmpItem.pos	=	i;
				tmpRow.addItem(tmpItem);		//添加完整一条数据
			}
			tmpRow.addEventListener(TableEvent.ROW_SELECT,onItemClick);//事件
			
			tmpRow.pos	=	index;
			_rowArr.splice(index,0,tmpRow)			//添加一条数据
			tmpRow.data	=	rowData;
			tmpRow.y	=	_bodySpace.height + rowSplit;
			_bodySpace.addChild(tmpRow);
			if(_maxWidth<tmpRow.itemPox)
			{
				_maxWidth	=	tmpRow.itemPox;
				if(_selectShap.numChildren==0)
				{
					//画选中效果
					DrawAPI.drawRect(_selectShap.graphics,DrawStyle.getRectStyle(0,0,_maxWidth,20),DrawStyle.getFillStyle(uint(0xC3B61A),.7));
				}
			}
			updataBodySpace();
			
			
			
		}
		
		public function addRow(labelArr:Array,rowData:BaseData=null):void
		{
			var tmpRow:TableRow	=	new TableRow();		//一条数据
			for(var i:int=0;i<labelArr.length;i++)
			{
				var tmpItem:TableItem	=	new TableItem(labelArr[i],false,_sizeData[i]);//TableItem就是一个Label		_sizeData[idx]列之间间隙
				tmpItem.pos	=	i;
				tmpRow.addItem(tmpItem);		//添加完整一条数据
			}
			tmpRow.addEventListener(TableEvent.ROW_SELECT,onItemClick);//事件
			tmpRow.addEventListener(TableEvent.ITEM_LINK,onItemClick);
			tmpRow.pos	=	_rowArr.length;
			_rowArr.push(tmpRow);			//添加一条数据
			tmpRow.data	=	rowData;
			tmpRow.y	=	_bodySpace.height + rowSplit;
			_bodySpace.addChild(tmpRow);
			if(_maxWidth<tmpRow.itemPox)
			{
				_maxWidth	=	tmpRow.itemPox;
				if(_selectShap.numChildren==0)
				{
					//画选中效果
					DrawAPI.drawRect(_selectShap.graphics,DrawStyle.getRectStyle(0,0,_maxWidth,20),DrawStyle.getFillStyle(uint(0xC3B61A),.7));
				}
			}
			updataBodySpace();
		}
		
		/**
		 * 添加事件 
		 */
		public function addItemEvent(pType:String,pListener:Function):void
		{
			_callBack.HandleProperties(pType,pListener);
			for each(var item:TableRow in _rowArr)
			{
//				if(!item.hasEventListener(pType))
					item.addEventListener(pType, onEvent);
			}
		}
		
		private function onEvent(evt:Event):void
		{
			_callBack.GetProperties(evt.type).call(this,evt,evt.currentTarget);
		}
		
		/**
		 *	删除某一行 数据 
		 * @param index
		 * 
		 */		
		public function removeRowByIndex(idx:int):void
		{
			_rowArr.splice(idx,1);		//删除数据
			updataBodySpace();			//重绘
		}
		
		public function removeSelectRow():void
		{
			if(_selectRow!=null)
			{
				var tmpIdx:int	=	_selectRow.pos;
				removeRowByIndex(tmpIdx);
			}
		}
		
		/**
		 *	重绘Table Body容器 
		 */		
		public function updataBodySpace():void
		{
			cleanbodySpace();
			var len:int	=	_rowArr.length;
			if(_maxWidth<_bodySpace.width)
			{
				_maxWidth	=	_bodySpace.width;
			}
			for(var i:int	=0;i<len;i++)
			{
				var tmpRow:TableRow	=	_rowArr[i];
				var txtBg	: MovieClip;
				tmpRow.pos	=	i;
				tmpRow.y	=	(_lineHeight + _rowSpace) * i + rowSplit;
//				tmpRow.y	=	_bodySpace.height + Table.rowSplit;
				_bodySpace.addChild(tmpRow);				
			}
			
			_scrollPanel.update();			//滚动条更新
		}
		/**
		 *	清空Table Body 容器
		 */		
		private function cleanbodySpace():void
		{
			while(_bodySpace.numChildren>0)
			{
				_bodySpace.removeChildAt(0);
			}
		}
		/**
		 * 	清空列表
		 */		
		public function clear():void
		{
			cleanbodySpace();
			_rowArr	=	[];
		}
		
		/**
		 * 选中一行 
		 * @param evt
		 */
		private function onItemClick(evt:TableEvent):void
		{
			var index			:int;
			if(_bodySpace.contains(_selectShap))
			{
				index		=	_bodySpace.numChildren	-	_rowArr.length	-1; 
			}
			else
			{
				index	=	_bodySpace.numChildren	-	_rowArr.length;
			}
			
			_bodySpace.addChildAt(_selectShap,index);
			_selectShap.alpha	=	1;
			_selectShap.y	=	evt.currentTarget.y;//选中框移动
			_selectRow	=	evt.currentTarget as TableRow;
			dispatchEvent(evt);
		}

		/**
		 *	获取行信息 
		 * @return 
		 * 
		 */		
		public function get rowArr():Array
		{
			return _rowArr;
		}

		public function get bodySpace():Sprite
		{
			return _bodySpace;
		}

		public function set bodySpace(value:Sprite):void
		{
			_bodySpace = value;
		}

		
		/**
		 *	选择效果设定 
		 * @return 
		 * 
		 */		
		public function get selectShap():Sprite
		{
			return _selectShap;
		}

		public function set selectShap(value:Sprite):void
		{
			_selectShap.graphics.clear();
			while(_selectShap.numChildren)
			{
				_selectShap.removeChildAt(0);
			}
			_selectShap.addChild(value);
			
		}
		
		/**
		 * 表头和表内容之间的间隔 
		 * @param val
		 * 
		 */
		public function set topSpace(val:int):void
		{
			rowSplit = val;
			updataBodySpace();
		}
		
		/**
		 * 行间距，值不包含默认高度19 
		 * @param val
		 * 
		 */
		public function set rowSpace(val:int):void
		{
			_rowSpace = val;
			updataBodySpace();
		}
		public function get selectRow():TableRow
		{
			return _selectRow;
		}
	}
}